package acme.bloodtime.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.IArmaduraDAO;

import dao.api.DataObject;

/**
 * 
 * @author Manuel A. Quintero V.
 * 
 */

public class ArmaduraDAO extends ArticuloAbstractDAO implements IArmaduraDAO {

    public ArmaduraDAO() {
        // Empty
    }

    @Override
    public void createTable() throws SQLException {
        StringBuffer strbuf;

        ArticuloDAO articuloDAO = new ArticuloDAO();
        // ----------------------------------------

        dropTable();

        // ----------------------------------------
        strbuf = new StringBuffer();

        strbuf.append("CREATE TABLE ");
        strbuf.append(getTableName());
        strbuf.append(" (");
        strbuf.append(articuloDAO.getTableName() + ArmaduraDO.ID);
        strbuf.append("  INT REFERENCES ");
        strbuf.append(articuloDAO.getTableName());
        strbuf.append(" (");
        strbuf.append(ArmaduraDO.ID);
        strbuf.append("),");
        strbuf.append(ArmaduraDO.HAB_ARMAS_REQUERIDA);
        strbuf.append(" INT,    ");
        strbuf.append(ArmaduraDO.CANT_MEJORA_DEFENSA);
        strbuf.append(" INT,    ");
        strbuf.append(" PRIMARY KEY   ");
        strbuf.append(" (");
        strbuf.append(articuloDAO.getTableName() + ArmaduraDO.ID);
        strbuf.append("))");

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

    }

    @Override
    public void insert(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_INSERT);
        checkClass(dataObject, ArmaduraDO.class, CHECK_INSERT);

        ArmaduraDO armaduraDO = (ArmaduraDO) dataObject;

        armaduraDO.setId(internalInsertArticulo(dataObject, ArmaduraDO.class));
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("INSERT INTO ");
        strbuf.append(getTableName());
        strbuf.append(" VALUES (");
        strbuf.append(armaduraDO.getId());
        strbuf.append(", ");
        strbuf.append(armaduraDO.getHabArmasRequerida());
        strbuf.append(", ");
        strbuf.append(armaduraDO.getCantMejoraDefensa());
        strbuf.append(")");

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

        dtaSession.add(dataObject);

    }

    @Override
    public void update(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_UPDATE);
        checkClass(dataObject, ArmaduraDO.class, CHECK_UPDATE);

        ArticuloDAO articuloDAO = new ArticuloDAO();

        ArmaduraDO armaduraDO = (ArmaduraDO) dataObject;

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("UPDATE ");
        strbuf.append(getTableName());
        strbuf.append(" SET ");

        strbuf.append(ArmaduraDO.HAB_ARMAS_REQUERIDA);
        strbuf.append(" = ");
        strbuf.append(armaduraDO.getHabArmasRequerida());

        strbuf.append(", ");

        strbuf.append(ArmaduraDO.CANT_MEJORA_DEFENSA);
        strbuf.append(" = ");
        strbuf.append(armaduraDO.getCantMejoraDefensa());

        strbuf.append(" WHERE ");
        strbuf.append(articuloDAO.getTableName() + ArmaduraDO.ID);
        strbuf.append(" = ");
        strbuf.append(armaduraDO.getId());

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

        internalUpdateArticulo(dataObject, ArmaduraDO.class);

    }

    @Override
    public void delete(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_DELETE);
        checkClass(dataObject, ArmaduraDO.class, CHECK_DELETE);

        ArticuloDAO articuloDAO = new ArticuloDAO();

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("DELETE FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(articuloDAO.getTableName() + DataObject.ID);
        strbuf.append(" = ");
        strbuf.append(dataObject.getId());

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());
        internalDelete(dataObject, ArmaduraDO.class);
        dtaSession.del(dataObject);

    }

    @Override
    protected DataObject resultSetToDO(ResultSet rs) throws SQLException {
        ArmaduraDO ret = //
        (ArmaduraDO) dtaSession.getDtaByKey( //
                ArmaduraDO.class, rs.getInt(ArmaduraDO.ID));
        if (ret != null) {
            return ret;
        }
        ret = new ArmaduraDO();

        ret.setId/*					*/(rs.getInt(ArmaduraDO.ID));
        ret.setNombre/*				*/(rs.getString(ArmaduraDO.NOMBRE));
        ret.setDescripcion/*		*/(rs.getString(ArmaduraDO.DESCRIPCION));
        ret.setImagen/*				*/(rs.getBytes(ArmaduraDO.IMAGEN));
        ret.setPrecioCompra/*		*/(rs.getFloat(ArmaduraDO.PRECIO_COMPRA));
        ret.setPrecioVenta/*		*/(rs.getFloat(ArmaduraDO.PRECIO_VENTA));
        ret.setHabArmasRequerida/*	*/(rs.getInt(ArmaduraDO.HAB_ARMAS_REQUERIDA));
        ret.setCantMejoraDefensa/*	*/(rs.getInt(ArmaduraDO.CANT_MEJORA_DEFENSA));

        return (ArmaduraDO) dtaSession.add(ret);
    }

}